package com.buaa.volleyTest;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.LruCache;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private NetworkImageView netWorkImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        netWorkImageView = (NetworkImageView) findViewById(R.id.netWorkImageView);
        loadImageVolley();
        NetworkImageViewVolley();
        getJSONVolley();

    }

    @Override
    protected void onStop() {
        super.onStop();
        MyApplication.getHttpQueues().cancelAll("allTag");
    }

    private void loadImageVolley() {
        String imageUrl="http://img1.imgtn.bdimg.com/it/u=624374347,3315482130&fm=21&gp=0.jpg";
        RequestQueue requestQueue=Volley.newRequestQueue(this);
        final LruCache<String,Bitmap> lruCache=new LruCache<>(20);
        ImageLoader.ImageCache imageCache=new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return lruCache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
                lruCache.put(s,bitmap);
            }
        };
        ImageLoader imageLoader=new ImageLoader(requestQueue,imageCache);
        ImageLoader.ImageListener listener=imageLoader.getImageListener(imageView,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
        imageLoader.get(imageUrl,listener);
    }

    private void NetworkImageViewVolley() {
        String imageUrl="http://img1.imgtn.bdimg.com/it/u=624374347,3315482130&fm=21&gp=0.jpg";
        RequestQueue requestQueue=Volley.newRequestQueue(this);
        final LruCache<String,Bitmap> lruCache=new LruCache<>(20);
        ImageLoader.ImageCache imageCache=new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return lruCache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
                lruCache.put(s,bitmap);
            }
        };
        ImageLoader imageLoader=new ImageLoader(requestQueue,imageCache);
        netWorkImageView.setTag("allTag");
        netWorkImageView.setImageUrl(imageUrl,imageLoader);
    }

    private void getJSONVolley() {
        RequestQueue requesQueue = Volley.newRequestQueue(this);
        String JsonDataUrl = "http://192.168.1.105:8080/jsonProject/jsonAction?action_flag=person";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JsonDataUrl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.i("sgk","jsonObject-----------"+jsonObject);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.i("sgk","网络请求失败，请重试!");
            }
        });
        jsonObjectRequest.setTag("allTag");
        requesQueue.add(jsonObjectRequest);
    }


}
